package com.mobapping;

import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Handler;
import android.util.Log;

public class WebAdapter implements IDataAdapter {
	
	private static final String NAMESPACE =  "http://mobapping.com"; //"http://10.0.2.2/soap";
	
	private static final String pharmacyURL = "http://www.mekya.com/mobapping/server.php";//"http://www.mekya.com/mobapping/test/MobAppingWebService/server.php";  //"http://10.0.2.2/soap/server.php"; //	
	//private static final String pharmacyURL = "http://10.0.2.2/soap/server.php"; //	

	
	public void getElements(final int id,final IArrayListDataListener dataListener) 
	{
		Thread thread = new Thread()
		{
			Handler handler = new Handler();
			ArrayList<Object> elements = new ArrayList<Object>();
			@Override
			public void run() {
				final String METHOD_NAME = "getElements";
				final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				request.addProperty("parentId", id);
				 
		        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        envelope.dotNet = true;
		        envelope.setOutputSoapObject(request);		     
		        HttpTransportSE androidHttpTransport;
		        try {
		        	 androidHttpTransport = new HttpTransportSE(pharmacyURL);
		        	androidHttpTransport.debug = true;
		        	    		   
		            androidHttpTransport.call(SOAP_ACTION, envelope);
		     
		            Vector getElementsResult =   (Vector)envelope.getResponse();
		            
		            for(int i=0; i < getElementsResult.size(); i++) 
		            {
		            	SoapObject element = (SoapObject)getElementsResult.get(i);
		            	if( Boolean.parseBoolean(element.getProperty("isPharmacy").toString()) ) {
		            		elements.add(getPharmacyObject(element));
		            	}
		            	else {
		            		elements.add(getCountyObject(element));		            
		            	}
		            }
		            
		 
		        } catch(Exception E) {
		        	Log.i("e---xception", E.getMessage());  
		        }
		        
		        if (dataListener != null) {
		        	
		        	handler.post(new Runnable() {
						public void run() {
							dataListener.dataArrived(elements);
						}
					});	
		        }
			}
		};
		thread.start();	
	}
	
	@Override
	public void search(final String countyName, final int parentId,
			final IArrayListDataListener dataListener) {
		
		Thread thread = new Thread()
		{
			Handler handler = new Handler();
			ArrayList<Object> pharmacies = new ArrayList<Object>();
			@Override
			public void run() {
				final String METHOD_NAME = "search";
				final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				request.addProperty("countyName", countyName);
				request.addProperty("parentId", parentId);
				 
		        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        envelope.dotNet = true;
		        envelope.setOutputSoapObject(request);		     
		        
		        try {
		        	HttpTransportSE androidHttpTransport = new HttpTransportSE(pharmacyURL);
		        	androidHttpTransport.debug = true;
		        	    		   
		            androidHttpTransport.call(SOAP_ACTION, envelope);
		     
		            Vector getPharmaciesResult =   (Vector)envelope.getResponse();
		            
		            for(int i=0; i < getPharmaciesResult.size(); i++) 
		            {
		            	SoapObject element = (SoapObject)getPharmaciesResult.get(i);
		            	pharmacies.add(getPharmacyObject(element));	
		            }
		  
		        } catch(Exception E) {
		        	Log.i("e---xception", E.getMessage());  
		        }
		        
		        if (dataListener != null) {
		        	
		        	handler.post(new Runnable() {
						public void run() {
							dataListener.dataArrived(pharmacies);
						}
					});
		        }
				
			}
		};
		thread.start();
	}

	@Override
	public void search(final double lattitude, final double longitude, final int range,
			final IArrayListDataListener dataListener) {

		Thread thread = new Thread()
		{
			Handler handler = new Handler();
			ArrayList<Object> pharmacies = new ArrayList<Object>();
			@Override
			public void run() {
				final String METHOD_NAME = "searchPlace";
				final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				request.addProperty("lattitude", new SoapPrimitive( "http://www.w3.org/2001/XMLSchema","double",Double.toString(lattitude)));
				request.addProperty("longitude", new SoapPrimitive( "http://www.w3.org/2001/XMLSchema","double",Double.toString(longitude)));
				request.addProperty("range", range);
				 
		        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        envelope.dotNet = true;
		        envelope.setOutputSoapObject(request);		     
		        
		        try {
		        	HttpTransportSE androidHttpTransport = new HttpTransportSE(pharmacyURL);
		        	androidHttpTransport.debug = true;
		        	    		   
		            androidHttpTransport.call(SOAP_ACTION, envelope);
		     
		            Vector getPharmaciesResult =   (Vector)envelope.getResponse();
		            
		            for(int i=0; i < getPharmaciesResult.size(); i++) 
		            {
		            	SoapObject element = (SoapObject)getPharmaciesResult.get(i);		            	
		            	pharmacies.add(getPharmacyObject(element));
		            }
		  
		 
		        } catch(Exception E) {
		        	Log.i("e---xception", E.getMessage());  
		        }
		        
		        if (dataListener != null) {
		        	
		        	handler.post(new Runnable() {
						public void run() {
							dataListener.dataArrived(pharmacies);
						}
					});
		        }
				
			}
		};
		thread.start();
	}

	
	private ElementInfo getCountyObject(SoapObject object) 
	{
	    try {
	    	return new ElementInfo((String)object.getProperty("Name"), null, null, Boolean.parseBoolean(object.getProperty("isPharmacy").toString()) ,Integer.parseInt(object.getProperty("id").toString()), Double.parseDouble(object.getProperty("lattitude").toString()), Double.parseDouble(object.getProperty("longitude").toString()),(String) object.getProperty("URL"));
 
        } catch(Exception E) {
        	return new ElementInfo((String)object.getProperty("Name"), null, null, Boolean.parseBoolean(object.getProperty("isPharmacy").toString()) ,Integer.parseInt(object.getProperty("id").toString()), Double.parseDouble(object.getProperty("lattitude").toString()), Double.parseDouble(object.getProperty("longitude").toString()),null);
        	
        }
			
		
		
	
	}
	
	private ElementInfo getPharmacyObject(SoapObject object) 
	{
		 
	    try {
	    	return new ElementInfo((String)object.getProperty("Name"), (String)object.getProperty("Address"), (String)object.getProperty("Phone"), Boolean.parseBoolean(object.getProperty("isPharmacy").toString()), Integer.parseInt(object.getProperty("id").toString()), Double.parseDouble(object.getProperty("lattitude").toString()), Double.parseDouble(object.getProperty("longitude").toString()),(String)object.getProperty("URL"));
 
        } catch(Exception E) {
        	return new ElementInfo((String)object.getProperty("Name"), (String)object.getProperty("Address"), (String)object.getProperty("Phone"), Boolean.parseBoolean(object.getProperty("isPharmacy").toString()), Integer.parseInt(object.getProperty("id").toString()), Double.parseDouble(object.getProperty("lattitude").toString()), Double.parseDouble(object.getProperty("longitude").toString()),null);
        	
        }

	}
	
}
